app.factory('customObjectFilterFactory', ["$sessionStorage",'httpWrapService', function ($sessionStorage, httpWrapService) {
    function CustomObjectFilter(config){
        this.config = config;
    }

    CustomObjectFilter.prototype ={
        /**
         * 过滤
         */
        filter: function (input, filed) {
            var custom = this;
            var key = custom.getKey(input, filed);
            if(!input) {
                return '';
            }
            if(input && !$sessionStorage[key]) {
                httpWrapService.asyncPost(custom.getUrl(input, filed), custom.param, function(data){
                    $sessionStorage[key] = data.entity;
                });
            }
            return $sessionStorage[key][filed];
        },
        getUrl : function(input, filed){
            return this.config.getUrl(input, filed);
        },
        getKey : function(input, filed){
            return this.config.getKey(input, filed);
        }
    }

    /**
     * 新建实例
     * @param config
     * @returns {Grid}
     */
    CustomObjectFilter.newInstance = function(config){
        return new CustomObjectFilter(config);
    }

    return {
        newInstance : function(config){
            return CustomObjectFilter.newInstance(config);
        }
    };
}]);

/**
 * 字符串转为时间
 */
app.filter('date_parse', [function() {
    return  function (input) {
    	if(!input) {
    		return ;
    	}
        return moment(input, "YYYY-MM-DD HH:mm:ss").toDate();
    };
}]);

/**
 * 字符串转为long
 */
app.filter('date_parseNumber', [function() {
    return  function (input) {
    	if(!input) {
    		return ;
    	}
        return moment(input, "YYYY-MM-DD HH:mm:ss").toDate().getTime();
    };
}]);

/**
 * 地区过滤器
 */
app.filter('sys_region_Fiter', ["$sessionStorage", "httpWrapService","customObjectFilterFactory", function($sessionStorage, httpWrapService, customObjectFilterFactory) {
    return function(input, filed){
        return customObjectFilterFactory.newInstance({
            param : {},
            getUrl : function(intput, filed){
                return 'system/region/view/' +input+ '.do';
            },
            getKey : function(intput, filed){
                return 'sys_region_' + input;
            }
        }).filter(input, filed);
    };
}]);
/**
 * 机构过滤器
 */
app.filter('sys_group_Fiter', ["$sessionStorage", "httpWrapService","customObjectFilterFactory", function($sessionStorage, httpWrapService, customObjectFilterFactory) {
    return function(input, filed){
        return customObjectFilterFactory.newInstance({
            param : {},
            getUrl : function(intput, filed){
                return 'system/group/view/' +input+ '.do';
            },
            getKey : function(intput, filed){
                return 'sys_group_' + input;
            }
        }).filter(input, filed);
    };
}]);
/**
 * 用户过滤器
 */
app.filter('sys_user_Fiter', ["$sessionStorage", "httpWrapService","customObjectFilterFactory", function($sessionStorage, httpWrapService, customObjectFilterFactory) {
    return function(input, filed){
        return customObjectFilterFactory.newInstance({
            param : {},
            getUrl : function(intput, filed){
                return 'system/user/view/' +input+ '.do';
            },
            getKey : function(intput, filed){
                return 'sys_user_' + input;
            }
        }).filter(input, filed);
    };
}]);